package com.dfrz.problem.action.login;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.dispatcher.SessionMap;

import com.dfrz.problem.model.Employee;
import com.dfrz.problem.model.SysAdmin;
import com.dfrz.problem.model.User;
import com.dfrz.problem.service.RegisterService;
import com.dfrz.problem.service.SysAdminService;
import com.dfrz.problem.utils.IConstant;
import com.dfrz.problem.utils.Result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private RegisterService registerService;
	private SysAdminService sysAdminService;
	private Employee employee;
	private SysAdmin admin;
	private User user;
	
	/**
	 * 检验注册信息
	 * @return
	 */
	public boolean checkRegisterInfo(){
		registerService.create();
		
		if(employee == null){
			addActionError(getText(IConstant.REGISTER_INFO_NOT_NULL));
			return false;
		}
		boolean isSuccess = true;
		if(StringUtils.isEmpty(employee.getEmployeeEmail())){
			addFieldError("employee.employeeEmail", "邮箱账户不能为空.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getEmployeeName())){
			addFieldError("employee.employeeName", "用户姓名不能为空.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getPassword()) || StringUtils.isEmpty(employee.getAgainPwd())){
			addFieldError("employee.password", "密码输入不能为空.");
			isSuccess = false;
		}
		if(!employee.getAgainPwd().equals(employee.getPassword())){
			addActionError("两次输入的密码不同.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getBirthday())){
			addFieldError("employee.birthday", "出生日期不能为空.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getContact())){
			addFieldError("employee.contact", "联系电话不能为空.");
			isSuccess = false;
		}
		if(employee.getSex() != 1 && employee.getSex() != 2){
			addFieldError("employee.set", "必须选择性别.");
			isSuccess = false;
		}
			
		return isSuccess;
	}
	
	/**
	 * 用户注册校验
	 * @return
	 */
	public String checkReg(){
		// 注册信息填写有误
		if(!checkRegisterInfo()){
			return INPUT;
		}
		
		Result<Employee> result = registerService.addEmployee(employee);
		// 账户已存在
		if(IConstant.EXISTION_ACCOUNT.equals(result.getCode())){
			addActionError(getText(IConstant.EXISTION_ACCOUNT));
			return INPUT;
		}
		
		return SUCCESS;
	}
	
	public String addSysAdmin(){
		if(getUser()==null){
			return LOGIN;
		}
		return SUCCESS;
	}
	
	public String checkAddSysAdmin(){
		boolean isSuccess = true;
		if(StringUtils.isEmpty(admin.getAdminEmail())){
			addFieldError("admin.adminEmail", "管理员账户不能为空");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(admin.getPassword())){
			addFieldError("admin.password", "密码不能为空");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(admin.getAdminName())){
			addFieldError("admin.adminName", "管理员姓名不能为空");
			isSuccess = false;
		}
		if(!admin.getPassword().equals(admin.getAgainPwd())){
			addActionError("两次输入的密码不同");
			isSuccess = false;
		}
		if(!isSuccess){
			return INPUT;
		}
		
		
		Result<SysAdmin> result = sysAdminService.addSysAdmin(admin);
		if(IConstant.SYSTEMADMIN_ALREADY_EXIST.equals(result.getCode())){
			addActionError(getText(IConstant.SYSTEMADMIN_ALREADY_EXIST));
			return INPUT;
		}
		if(IConstant.SYSTEMADMIN_MAXIMUM_3.equals(result.getCode())){
			addActionError(getText(IConstant.SYSTEMADMIN_MAXIMUM_3));
			return INPUT;
		}
		
		return SUCCESS;
	}
	
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	
	public void setRegisterService(RegisterService registerService) {
		this.registerService = registerService;
	}
	
	public RegisterService getRegisterService() {
		return registerService;
	}
	
	public Employee getEmployee() {
		return employee;
	}
	
	public void setSysAdminService(SysAdminService sysAdminService) {
		this.sysAdminService = sysAdminService;
	}
	
	public SysAdmin getAdmin() {
		return admin;
	}
	
	public void setAdmin(SysAdmin admin) {
		this.admin = admin;
	}
	
	public User getUser(){
		SessionMap<String, Object> session = (SessionMap<String, Object>) ActionContext
				.getContext().getSession();
		user = (User) session.get("user");
		return user;
	}
}
